package com.zdyl.fileReplaceTool;

import com.alibaba.fastjson.JSON;
import com.zdyl.fileReplaceTool.utils.CommonUtils;
import org.apache.log4j.Logger;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version V1.0
 * @Title:
 * @Package
 * @Description:
 * @author: runoob
 * @date:
 */
public class Start {

    public static Logger logger = Logger.getLogger(Start.class);

    public String matchStr = "";
    public List<String> filePaths = new ArrayList<>();
    public String oldStr = "";
    public String newStr = "";
    private CommonUtils commonUtils = new CommonUtils();

    public String start(Boolean isNameCase,Boolean isStrCase){
        StringBuilder info = new StringBuilder();
        String config = commonUtils.getConfig(new File(System.getProperty("user.dir") + "/filemtconfig"));
        try {
            if (config == null || config.equals("")) {
                logger.error("配置文件获取为空！");
                info.append("配置文件获取为空！").append("\n");
                return info.toString();
            }
            Map map = JSON.parseObject(config, Map.class);
            filePaths = (List<String>) map.get("filePaths");
            logger.info("获取到配置的路径:" + JSON.toJSONString(filePaths));
            info.append("获取到配置的路径:").append("\n");
            for (String path : filePaths){
                info.append(path).append("\n");
            }
            matchStr = map.get("matchStr").toString();
            oldStr = map.get("oldStr").toString();
            newStr = map.get("newStr").toString();
            info.append(doModify(isNameCase,isStrCase)).append("\n");
        } catch (Exception e) {
            logger.error("发生异常", e);
        }
        return info.toString();
    }

    public String start(String pathCache,String name,String oldStrCache,String newStrCache,Boolean isNameCase,Boolean isStrCase){
        StringBuilder info = new StringBuilder();
        try {
            String[] pathStrings = pathCache.split(",");
            for (int i=0;i<pathStrings.length;i++){
                filePaths.add(i,pathStrings[i]);
            }
            logger.info("获取到输入的路径:" + JSON.toJSONString(filePaths));
            info.append("获取到输入的路径:").append("\n");
            for (String path : filePaths){
                info.append(path).append("\n");
            }
            matchStr = name;
            oldStr = oldStrCache;
            newStr = newStrCache;
            info.append(doModify(isNameCase,isStrCase)).append("\n");
        } catch (Exception e) {
            logger.error("发生异常", e);
        }
        return info.toString();
    }

    public String doModify(Boolean isNameCase,Boolean isStrCase){
        StringBuilder infoCache = new StringBuilder();
        List<Map> infoList = new ArrayList();
        int i = 0;
        int count = 0;
        for (String path : filePaths) {
            for (String matchStrCache : matchStr.split(",")) {
                Map<String, Object> filesName = commonUtils.getFilesName(path, matchStrCache, isNameCase);
                List<String> fileNameList = (List<String>) filesName.get("fileNameList");
                logger.info("目录" + path + "下获取的文件列表" + JSON.toJSONString(fileNameList));
                infoCache.append("\n").append("目录").append(path).append("下获取的文件列表").append("\n");
                for (String listCache : fileNameList){
                    infoCache.append(listCache).append("\n");
                }
                for (String fileName : fileNameList) {
                    int cache = commonUtils.modifyFileContent(new File(fileName),oldStr,newStr,isStrCase);
                    Map<String, String> info = new HashMap<>();
                    info.put("name",fileName);
                    info.put("count",String.valueOf(cache));
                    infoList.add(i,info);
                    i++;
                    count+=cache;
                }
            }
        }
        Main.flag = true;
        logger.info("共遍历了" + i + "个文件，共替换了" + count + "处。");
        infoCache.append("\n").append("共遍历了").append(i).append("个文件，共替换了").append(count).append("处。").append("\n");
        for (Map cache  : infoList){
            logger.info("文件：" + cache.get("name") + "替换了" + cache.get("count") + "处。");
            infoCache.append("文件：").append(cache.get("name")).append("替换了").append(cache.get("count")).append("处。").append("\n");
        }
        return infoCache.toString();
    }

}
